/**
 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.crossriver.crgis.database.service.persistence;

import com.crossriver.crgis.database.NoSuchCompany_Contact_PersonException;
import com.crossriver.crgis.database.model.Company_Contact_Person;
import com.crossriver.crgis.database.model.impl.Company_Contact_PersonImpl;
import com.crossriver.crgis.database.model.impl.Company_Contact_PersonModelImpl;

import com.liferay.portal.NoSuchModelException;
import com.liferay.portal.kernel.annotation.BeanReference;
import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.BatchSessionUtil;
import com.liferay.portal.service.persistence.ResourcePersistence;
import com.liferay.portal.service.persistence.UserPersistence;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The persistence implementation for the company_ contact_ person service.
 *
 * <p>
 * Never modify or reference this class directly. Always use {@link Company_Contact_PersonUtil} to access the company_ contact_ person persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
 * </p>
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author Wole Adetiba
 * @see Company_Contact_PersonPersistence
 * @see Company_Contact_PersonUtil
 * @generated
 */
public class Company_Contact_PersonPersistenceImpl extends BasePersistenceImpl<Company_Contact_Person>
	implements Company_Contact_PersonPersistence {
	public static final String FINDER_CLASS_NAME_ENTITY = Company_Contact_PersonImpl.class.getName();
	public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
		".List";
	public static final FinderPath FINDER_PATH_FIND_BY_USERID = new FinderPath(Company_Contact_PersonModelImpl.ENTITY_CACHE_ENABLED,
			Company_Contact_PersonModelImpl.FINDER_CACHE_ENABLED,
			FINDER_CLASS_NAME_LIST, "findByUserId",
			new String[] {
				String.class.getName(),
				
			"java.lang.Integer", "java.lang.Integer",
				"com.liferay.portal.kernel.util.OrderByComparator"
			});
	public static final FinderPath FINDER_PATH_COUNT_BY_USERID = new FinderPath(Company_Contact_PersonModelImpl.ENTITY_CACHE_ENABLED,
			Company_Contact_PersonModelImpl.FINDER_CACHE_ENABLED,
			FINDER_CLASS_NAME_LIST, "countByUserId",
			new String[] { String.class.getName() });
	public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(Company_Contact_PersonModelImpl.ENTITY_CACHE_ENABLED,
			Company_Contact_PersonModelImpl.FINDER_CACHE_ENABLED,
			FINDER_CLASS_NAME_LIST, "findAll", new String[0]);
	public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(Company_Contact_PersonModelImpl.ENTITY_CACHE_ENABLED,
			Company_Contact_PersonModelImpl.FINDER_CACHE_ENABLED,
			FINDER_CLASS_NAME_LIST, "countAll", new String[0]);

	/**
	 * Caches the company_ contact_ person in the entity cache if it is enabled.
	 *
	 * @param company_Contact_Person the company_ contact_ person to cache
	 */
	public void cacheResult(Company_Contact_Person company_Contact_Person) {
		EntityCacheUtil.putResult(Company_Contact_PersonModelImpl.ENTITY_CACHE_ENABLED,
			Company_Contact_PersonImpl.class,
			company_Contact_Person.getPrimaryKey(), company_Contact_Person);
	}

	/**
	 * Caches the company_ contact_ persons in the entity cache if it is enabled.
	 *
	 * @param company_Contact_Persons the company_ contact_ persons to cache
	 */
	public void cacheResult(
		List<Company_Contact_Person> company_Contact_Persons) {
		for (Company_Contact_Person company_Contact_Person : company_Contact_Persons) {
			if (EntityCacheUtil.getResult(
						Company_Contact_PersonModelImpl.ENTITY_CACHE_ENABLED,
						Company_Contact_PersonImpl.class,
						company_Contact_Person.getPrimaryKey(), this) == null) {
				cacheResult(company_Contact_Person);
			}
		}
	}

	/**
	 * Clears the cache for all company_ contact_ persons.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	public void clearCache() {
		CacheRegistryUtil.clear(Company_Contact_PersonImpl.class.getName());
		EntityCacheUtil.clearCache(Company_Contact_PersonImpl.class.getName());
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
	}

	/**
	 * Clears the cache for the company_ contact_ person.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	public void clearCache(Company_Contact_Person company_Contact_Person) {
		EntityCacheUtil.removeResult(Company_Contact_PersonModelImpl.ENTITY_CACHE_ENABLED,
			Company_Contact_PersonImpl.class,
			company_Contact_Person.getPrimaryKey());
	}

	/**
	 * Creates a new company_ contact_ person with the primary key. Does not add the company_ contact_ person to the database.
	 *
	 * @param applicationId the primary key for the new company_ contact_ person
	 * @return the new company_ contact_ person
	 */
	public Company_Contact_Person create(String applicationId) {
		Company_Contact_Person company_Contact_Person = new Company_Contact_PersonImpl();

		company_Contact_Person.setNew(true);
		company_Contact_Person.setPrimaryKey(applicationId);

		return company_Contact_Person;
	}

	/**
	 * Removes the company_ contact_ person with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param primaryKey the primary key of the company_ contact_ person to remove
	 * @return the company_ contact_ person that was removed
	 * @throws com.liferay.portal.NoSuchModelException if a company_ contact_ person with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Company_Contact_Person remove(Serializable primaryKey)
		throws NoSuchModelException, SystemException {
		return remove((String)primaryKey);
	}

	/**
	 * Removes the company_ contact_ person with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param applicationId the primary key of the company_ contact_ person to remove
	 * @return the company_ contact_ person that was removed
	 * @throws com.crossriver.crgis.database.NoSuchCompany_Contact_PersonException if a company_ contact_ person with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Company_Contact_Person remove(String applicationId)
		throws NoSuchCompany_Contact_PersonException, SystemException {
		Session session = null;

		try {
			session = openSession();

			Company_Contact_Person company_Contact_Person = (Company_Contact_Person)session.get(Company_Contact_PersonImpl.class,
					applicationId);

			if (company_Contact_Person == null) {
				if (_log.isWarnEnabled()) {
					_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + applicationId);
				}

				throw new NoSuchCompany_Contact_PersonException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
					applicationId);
			}

			return remove(company_Contact_Person);
		}
		catch (NoSuchCompany_Contact_PersonException nsee) {
			throw nsee;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	protected Company_Contact_Person removeImpl(
		Company_Contact_Person company_Contact_Person)
		throws SystemException {
		company_Contact_Person = toUnwrappedModel(company_Contact_Person);

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.delete(session, company_Contact_Person);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);

		EntityCacheUtil.removeResult(Company_Contact_PersonModelImpl.ENTITY_CACHE_ENABLED,
			Company_Contact_PersonImpl.class,
			company_Contact_Person.getPrimaryKey());

		return company_Contact_Person;
	}

	public Company_Contact_Person updateImpl(
		com.crossriver.crgis.database.model.Company_Contact_Person company_Contact_Person,
		boolean merge) throws SystemException {
		company_Contact_Person = toUnwrappedModel(company_Contact_Person);

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.update(session, company_Contact_Person, merge);

			company_Contact_Person.setNew(false);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);

		EntityCacheUtil.putResult(Company_Contact_PersonModelImpl.ENTITY_CACHE_ENABLED,
			Company_Contact_PersonImpl.class,
			company_Contact_Person.getPrimaryKey(), company_Contact_Person);

		return company_Contact_Person;
	}

	protected Company_Contact_Person toUnwrappedModel(
		Company_Contact_Person company_Contact_Person) {
		if (company_Contact_Person instanceof Company_Contact_PersonImpl) {
			return company_Contact_Person;
		}

		Company_Contact_PersonImpl company_Contact_PersonImpl = new Company_Contact_PersonImpl();

		company_Contact_PersonImpl.setNew(company_Contact_Person.isNew());
		company_Contact_PersonImpl.setPrimaryKey(company_Contact_Person.getPrimaryKey());

		company_Contact_PersonImpl.setApplicationId(company_Contact_Person.getApplicationId());
		company_Contact_PersonImpl.setUserId(company_Contact_Person.getUserId());
		company_Contact_PersonImpl.setDesignation(company_Contact_Person.getDesignation());
		company_Contact_PersonImpl.setTitle(company_Contact_Person.getTitle());
		company_Contact_PersonImpl.setLastName(company_Contact_Person.getLastName());
		company_Contact_PersonImpl.setFirstName(company_Contact_Person.getFirstName());
		company_Contact_PersonImpl.setMiddleName(company_Contact_Person.getMiddleName());
		company_Contact_PersonImpl.setPhoneHome(company_Contact_Person.getPhoneHome());
		company_Contact_PersonImpl.setPhoneOffice(company_Contact_Person.getPhoneOffice());
		company_Contact_PersonImpl.setPhoneMobile(company_Contact_Person.getPhoneMobile());
		company_Contact_PersonImpl.setFax(company_Contact_Person.getFax());
		company_Contact_PersonImpl.setEmailAddress(company_Contact_Person.getEmailAddress());
		company_Contact_PersonImpl.setIdentificationDoc(company_Contact_Person.getIdentificationDoc());
		company_Contact_PersonImpl.setIdentificationDocNo(company_Contact_Person.getIdentificationDocNo());

		return company_Contact_PersonImpl;
	}

	/**
	 * Finds the company_ contact_ person with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
	 *
	 * @param primaryKey the primary key of the company_ contact_ person to find
	 * @return the company_ contact_ person
	 * @throws com.liferay.portal.NoSuchModelException if a company_ contact_ person with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Company_Contact_Person findByPrimaryKey(Serializable primaryKey)
		throws NoSuchModelException, SystemException {
		return findByPrimaryKey((String)primaryKey);
	}

	/**
	 * Finds the company_ contact_ person with the primary key or throws a {@link com.crossriver.crgis.database.NoSuchCompany_Contact_PersonException} if it could not be found.
	 *
	 * @param applicationId the primary key of the company_ contact_ person to find
	 * @return the company_ contact_ person
	 * @throws com.crossriver.crgis.database.NoSuchCompany_Contact_PersonException if a company_ contact_ person with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Company_Contact_Person findByPrimaryKey(String applicationId)
		throws NoSuchCompany_Contact_PersonException, SystemException {
		Company_Contact_Person company_Contact_Person = fetchByPrimaryKey(applicationId);

		if (company_Contact_Person == null) {
			if (_log.isWarnEnabled()) {
				_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + applicationId);
			}

			throw new NoSuchCompany_Contact_PersonException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
				applicationId);
		}

		return company_Contact_Person;
	}

	/**
	 * Finds the company_ contact_ person with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param primaryKey the primary key of the company_ contact_ person to find
	 * @return the company_ contact_ person, or <code>null</code> if a company_ contact_ person with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Company_Contact_Person fetchByPrimaryKey(Serializable primaryKey)
		throws SystemException {
		return fetchByPrimaryKey((String)primaryKey);
	}

	/**
	 * Finds the company_ contact_ person with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param applicationId the primary key of the company_ contact_ person to find
	 * @return the company_ contact_ person, or <code>null</code> if a company_ contact_ person with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Company_Contact_Person fetchByPrimaryKey(String applicationId)
		throws SystemException {
		Company_Contact_Person company_Contact_Person = (Company_Contact_Person)EntityCacheUtil.getResult(Company_Contact_PersonModelImpl.ENTITY_CACHE_ENABLED,
				Company_Contact_PersonImpl.class, applicationId, this);

		if (company_Contact_Person == null) {
			Session session = null;

			try {
				session = openSession();

				company_Contact_Person = (Company_Contact_Person)session.get(Company_Contact_PersonImpl.class,
						applicationId);
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (company_Contact_Person != null) {
					cacheResult(company_Contact_Person);
				}

				closeSession(session);
			}
		}

		return company_Contact_Person;
	}

	/**
	 * Finds all the company_ contact_ persons where userId = &#63;.
	 *
	 * @param userId the user id to search with
	 * @return the matching company_ contact_ persons
	 * @throws SystemException if a system exception occurred
	 */
	public List<Company_Contact_Person> findByUserId(String userId)
		throws SystemException {
		return findByUserId(userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
	}

	/**
	 * Finds a range of all the company_ contact_ persons where userId = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param userId the user id to search with
	 * @param start the lower bound of the range of company_ contact_ persons to return
	 * @param end the upper bound of the range of company_ contact_ persons to return (not inclusive)
	 * @return the range of matching company_ contact_ persons
	 * @throws SystemException if a system exception occurred
	 */
	public List<Company_Contact_Person> findByUserId(String userId, int start,
		int end) throws SystemException {
		return findByUserId(userId, start, end, null);
	}

	/**
	 * Finds an ordered range of all the company_ contact_ persons where userId = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param userId the user id to search with
	 * @param start the lower bound of the range of company_ contact_ persons to return
	 * @param end the upper bound of the range of company_ contact_ persons to return (not inclusive)
	 * @param orderByComparator the comparator to order the results by
	 * @return the ordered range of matching company_ contact_ persons
	 * @throws SystemException if a system exception occurred
	 */
	public List<Company_Contact_Person> findByUserId(String userId, int start,
		int end, OrderByComparator orderByComparator) throws SystemException {
		Object[] finderArgs = new Object[] {
				userId,
				
				String.valueOf(start), String.valueOf(end),
				String.valueOf(orderByComparator)
			};

		List<Company_Contact_Person> list = (List<Company_Contact_Person>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_USERID,
				finderArgs, this);

		if (list == null) {
			StringBundler query = null;

			if (orderByComparator != null) {
				query = new StringBundler(3 +
						(orderByComparator.getOrderByFields().length * 3));
			}
			else {
				query = new StringBundler(3);
			}

			query.append(_SQL_SELECT_COMPANY_CONTACT_PERSON_WHERE);

			if (userId == null) {
				query.append(_FINDER_COLUMN_USERID_USERID_1);
			}
			else {
				if (userId.equals(StringPool.BLANK)) {
					query.append(_FINDER_COLUMN_USERID_USERID_3);
				}
				else {
					query.append(_FINDER_COLUMN_USERID_USERID_2);
				}
			}

			if (orderByComparator != null) {
				appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
					orderByComparator);
			}

			else {
				query.append(Company_Contact_PersonModelImpl.ORDER_BY_JPQL);
			}

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				if (userId != null) {
					qPos.add(userId);
				}

				list = (List<Company_Contact_Person>)QueryUtil.list(q,
						getDialect(), start, end);
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					FinderCacheUtil.removeResult(FINDER_PATH_FIND_BY_USERID,
						finderArgs);
				}
				else {
					cacheResult(list);

					FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_USERID,
						finderArgs, list);
				}

				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Finds the first company_ contact_ person in the ordered set where userId = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param userId the user id to search with
	 * @param orderByComparator the comparator to order the set by
	 * @return the first matching company_ contact_ person
	 * @throws com.crossriver.crgis.database.NoSuchCompany_Contact_PersonException if a matching company_ contact_ person could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Company_Contact_Person findByUserId_First(String userId,
		OrderByComparator orderByComparator)
		throws NoSuchCompany_Contact_PersonException, SystemException {
		List<Company_Contact_Person> list = findByUserId(userId, 0, 1,
				orderByComparator);

		if (list.isEmpty()) {
			StringBundler msg = new StringBundler(4);

			msg.append(_NO_SUCH_ENTITY_WITH_KEY);

			msg.append("userId=");
			msg.append(userId);

			msg.append(StringPool.CLOSE_CURLY_BRACE);

			throw new NoSuchCompany_Contact_PersonException(msg.toString());
		}
		else {
			return list.get(0);
		}
	}

	/**
	 * Finds the last company_ contact_ person in the ordered set where userId = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param userId the user id to search with
	 * @param orderByComparator the comparator to order the set by
	 * @return the last matching company_ contact_ person
	 * @throws com.crossriver.crgis.database.NoSuchCompany_Contact_PersonException if a matching company_ contact_ person could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Company_Contact_Person findByUserId_Last(String userId,
		OrderByComparator orderByComparator)
		throws NoSuchCompany_Contact_PersonException, SystemException {
		int count = countByUserId(userId);

		List<Company_Contact_Person> list = findByUserId(userId, count - 1,
				count, orderByComparator);

		if (list.isEmpty()) {
			StringBundler msg = new StringBundler(4);

			msg.append(_NO_SUCH_ENTITY_WITH_KEY);

			msg.append("userId=");
			msg.append(userId);

			msg.append(StringPool.CLOSE_CURLY_BRACE);

			throw new NoSuchCompany_Contact_PersonException(msg.toString());
		}
		else {
			return list.get(0);
		}
	}

	/**
	 * Finds the company_ contact_ persons before and after the current company_ contact_ person in the ordered set where userId = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param applicationId the primary key of the current company_ contact_ person
	 * @param userId the user id to search with
	 * @param orderByComparator the comparator to order the set by
	 * @return the previous, current, and next company_ contact_ person
	 * @throws com.crossriver.crgis.database.NoSuchCompany_Contact_PersonException if a company_ contact_ person with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Company_Contact_Person[] findByUserId_PrevAndNext(
		String applicationId, String userId, OrderByComparator orderByComparator)
		throws NoSuchCompany_Contact_PersonException, SystemException {
		Company_Contact_Person company_Contact_Person = findByPrimaryKey(applicationId);

		Session session = null;

		try {
			session = openSession();

			Company_Contact_Person[] array = new Company_Contact_PersonImpl[3];

			array[0] = getByUserId_PrevAndNext(session, company_Contact_Person,
					userId, orderByComparator, true);

			array[1] = company_Contact_Person;

			array[2] = getByUserId_PrevAndNext(session, company_Contact_Person,
					userId, orderByComparator, false);

			return array;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	protected Company_Contact_Person getByUserId_PrevAndNext(Session session,
		Company_Contact_Person company_Contact_Person, String userId,
		OrderByComparator orderByComparator, boolean previous) {
		StringBundler query = null;

		if (orderByComparator != null) {
			query = new StringBundler(6 +
					(orderByComparator.getOrderByFields().length * 6));
		}
		else {
			query = new StringBundler(3);
		}

		query.append(_SQL_SELECT_COMPANY_CONTACT_PERSON_WHERE);

		if (userId == null) {
			query.append(_FINDER_COLUMN_USERID_USERID_1);
		}
		else {
			if (userId.equals(StringPool.BLANK)) {
				query.append(_FINDER_COLUMN_USERID_USERID_3);
			}
			else {
				query.append(_FINDER_COLUMN_USERID_USERID_2);
			}
		}

		if (orderByComparator != null) {
			String[] orderByFields = orderByComparator.getOrderByFields();

			if (orderByFields.length > 0) {
				query.append(WHERE_AND);
			}

			for (int i = 0; i < orderByFields.length; i++) {
				query.append(_ORDER_BY_ENTITY_ALIAS);
				query.append(orderByFields[i]);

				if ((i + 1) < orderByFields.length) {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(WHERE_GREATER_THAN_HAS_NEXT);
					}
					else {
						query.append(WHERE_LESSER_THAN_HAS_NEXT);
					}
				}
				else {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(WHERE_GREATER_THAN);
					}
					else {
						query.append(WHERE_LESSER_THAN);
					}
				}
			}

			query.append(ORDER_BY_CLAUSE);

			for (int i = 0; i < orderByFields.length; i++) {
				query.append(_ORDER_BY_ENTITY_ALIAS);
				query.append(orderByFields[i]);

				if ((i + 1) < orderByFields.length) {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(ORDER_BY_ASC_HAS_NEXT);
					}
					else {
						query.append(ORDER_BY_DESC_HAS_NEXT);
					}
				}
				else {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(ORDER_BY_ASC);
					}
					else {
						query.append(ORDER_BY_DESC);
					}
				}
			}
		}

		else {
			query.append(Company_Contact_PersonModelImpl.ORDER_BY_JPQL);
		}

		String sql = query.toString();

		Query q = session.createQuery(sql);

		q.setFirstResult(0);
		q.setMaxResults(2);

		QueryPos qPos = QueryPos.getInstance(q);

		if (userId != null) {
			qPos.add(userId);
		}

		if (orderByComparator != null) {
			Object[] values = orderByComparator.getOrderByValues(company_Contact_Person);

			for (Object value : values) {
				qPos.add(value);
			}
		}

		List<Company_Contact_Person> list = q.list();

		if (list.size() == 2) {
			return list.get(1);
		}
		else {
			return null;
		}
	}

	/**
	 * Finds all the company_ contact_ persons.
	 *
	 * @return the company_ contact_ persons
	 * @throws SystemException if a system exception occurred
	 */
	public List<Company_Contact_Person> findAll() throws SystemException {
		return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
	}

	/**
	 * Finds a range of all the company_ contact_ persons.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of company_ contact_ persons to return
	 * @param end the upper bound of the range of company_ contact_ persons to return (not inclusive)
	 * @return the range of company_ contact_ persons
	 * @throws SystemException if a system exception occurred
	 */
	public List<Company_Contact_Person> findAll(int start, int end)
		throws SystemException {
		return findAll(start, end, null);
	}

	/**
	 * Finds an ordered range of all the company_ contact_ persons.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of company_ contact_ persons to return
	 * @param end the upper bound of the range of company_ contact_ persons to return (not inclusive)
	 * @param orderByComparator the comparator to order the results by
	 * @return the ordered range of company_ contact_ persons
	 * @throws SystemException if a system exception occurred
	 */
	public List<Company_Contact_Person> findAll(int start, int end,
		OrderByComparator orderByComparator) throws SystemException {
		Object[] finderArgs = new Object[] {
				String.valueOf(start), String.valueOf(end),
				String.valueOf(orderByComparator)
			};

		List<Company_Contact_Person> list = (List<Company_Contact_Person>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
				finderArgs, this);

		if (list == null) {
			StringBundler query = null;
			String sql = null;

			if (orderByComparator != null) {
				query = new StringBundler(2 +
						(orderByComparator.getOrderByFields().length * 3));

				query.append(_SQL_SELECT_COMPANY_CONTACT_PERSON);

				appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
					orderByComparator);

				sql = query.toString();
			}
			else {
				sql = _SQL_SELECT_COMPANY_CONTACT_PERSON.concat(Company_Contact_PersonModelImpl.ORDER_BY_JPQL);
			}

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				if (orderByComparator == null) {
					list = (List<Company_Contact_Person>)QueryUtil.list(q,
							getDialect(), start, end, false);

					Collections.sort(list);
				}
				else {
					list = (List<Company_Contact_Person>)QueryUtil.list(q,
							getDialect(), start, end);
				}
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					FinderCacheUtil.removeResult(FINDER_PATH_FIND_ALL,
						finderArgs);
				}
				else {
					cacheResult(list);

					FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs,
						list);
				}

				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Removes all the company_ contact_ persons where userId = &#63; from the database.
	 *
	 * @param userId the user id to search with
	 * @throws SystemException if a system exception occurred
	 */
	public void removeByUserId(String userId) throws SystemException {
		for (Company_Contact_Person company_Contact_Person : findByUserId(
				userId)) {
			remove(company_Contact_Person);
		}
	}

	/**
	 * Removes all the company_ contact_ persons from the database.
	 *
	 * @throws SystemException if a system exception occurred
	 */
	public void removeAll() throws SystemException {
		for (Company_Contact_Person company_Contact_Person : findAll()) {
			remove(company_Contact_Person);
		}
	}

	/**
	 * Counts all the company_ contact_ persons where userId = &#63;.
	 *
	 * @param userId the user id to search with
	 * @return the number of matching company_ contact_ persons
	 * @throws SystemException if a system exception occurred
	 */
	public int countByUserId(String userId) throws SystemException {
		Object[] finderArgs = new Object[] { userId };

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_USERID,
				finderArgs, this);

		if (count == null) {
			StringBundler query = new StringBundler(2);

			query.append(_SQL_COUNT_COMPANY_CONTACT_PERSON_WHERE);

			if (userId == null) {
				query.append(_FINDER_COLUMN_USERID_USERID_1);
			}
			else {
				if (userId.equals(StringPool.BLANK)) {
					query.append(_FINDER_COLUMN_USERID_USERID_3);
				}
				else {
					query.append(_FINDER_COLUMN_USERID_USERID_2);
				}
			}

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				if (userId != null) {
					qPos.add(userId);
				}

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_USERID,
					finderArgs, count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Counts all the company_ contact_ persons.
	 *
	 * @return the number of company_ contact_ persons
	 * @throws SystemException if a system exception occurred
	 */
	public int countAll() throws SystemException {
		Object[] finderArgs = new Object[0];

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
				finderArgs, this);

		if (count == null) {
			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(_SQL_COUNT_COMPANY_CONTACT_PERSON);

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
					count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Initializes the company_ contact_ person persistence.
	 */
	public void afterPropertiesSet() {
		String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
					com.liferay.util.service.ServiceProps.get(
						"value.object.listener.com.crossriver.crgis.database.model.Company_Contact_Person")));

		if (listenerClassNames.length > 0) {
			try {
				List<ModelListener<Company_Contact_Person>> listenersList = new ArrayList<ModelListener<Company_Contact_Person>>();

				for (String listenerClassName : listenerClassNames) {
					listenersList.add((ModelListener<Company_Contact_Person>)InstanceFactory.newInstance(
							listenerClassName));
				}

				listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
			}
			catch (Exception e) {
				_log.error(e);
			}
		}
	}

	public void destroy() {
		EntityCacheUtil.removeCache(Company_Contact_PersonImpl.class.getName());
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
	}

	@BeanReference(type = Application_C_of_OPersistence.class)
	protected Application_C_of_OPersistence application_C_of_OPersistence;
	@BeanReference(type = Supporting_DocumentPersistence.class)
	protected Supporting_DocumentPersistence supporting_DocumentPersistence;
	@BeanReference(type = DocumentsPersistence.class)
	protected DocumentsPersistence documentsPersistence;
	@BeanReference(type = Individual_InformationPersistence.class)
	protected Individual_InformationPersistence individual_InformationPersistence;
	@BeanReference(type = Organisation_InformationPersistence.class)
	protected Organisation_InformationPersistence organisation_InformationPersistence;
	@BeanReference(type = Contact_InformationPersistence.class)
	protected Contact_InformationPersistence contact_InformationPersistence;
	@BeanReference(type = Company_Contact_PersonPersistence.class)
	protected Company_Contact_PersonPersistence company_Contact_PersonPersistence;
	@BeanReference(type = Property_InformationPersistence.class)
	protected Property_InformationPersistence property_InformationPersistence;
	@BeanReference(type = Held_PlotsPersistence.class)
	protected Held_PlotsPersistence held_PlotsPersistence;
	@BeanReference(type = History_Of_InformationPersistence.class)
	protected History_Of_InformationPersistence history_Of_InformationPersistence;
	@BeanReference(type = TransactionsPersistence.class)
	protected TransactionsPersistence transactionsPersistence;
	@BeanReference(type = Bank_Payment_SlipPersistence.class)
	protected Bank_Payment_SlipPersistence bank_Payment_SlipPersistence;
	@BeanReference(type = ResourcePersistence.class)
	protected ResourcePersistence resourcePersistence;
	@BeanReference(type = UserPersistence.class)
	protected UserPersistence userPersistence;
	private static final String _SQL_SELECT_COMPANY_CONTACT_PERSON = "SELECT company_Contact_Person FROM Company_Contact_Person company_Contact_Person";
	private static final String _SQL_SELECT_COMPANY_CONTACT_PERSON_WHERE = "SELECT company_Contact_Person FROM Company_Contact_Person company_Contact_Person WHERE ";
	private static final String _SQL_COUNT_COMPANY_CONTACT_PERSON = "SELECT COUNT(company_Contact_Person) FROM Company_Contact_Person company_Contact_Person";
	private static final String _SQL_COUNT_COMPANY_CONTACT_PERSON_WHERE = "SELECT COUNT(company_Contact_Person) FROM Company_Contact_Person company_Contact_Person WHERE ";
	private static final String _FINDER_COLUMN_USERID_USERID_1 = "company_Contact_Person.userId IS NULL";
	private static final String _FINDER_COLUMN_USERID_USERID_2 = "company_Contact_Person.userId = ?";
	private static final String _FINDER_COLUMN_USERID_USERID_3 = "(company_Contact_Person.userId IS NULL OR company_Contact_Person.userId = ?)";
	private static final String _ORDER_BY_ENTITY_ALIAS = "company_Contact_Person.";
	private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Company_Contact_Person exists with the primary key ";
	private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Company_Contact_Person exists with the key {";
	private static Log _log = LogFactoryUtil.getLog(Company_Contact_PersonPersistenceImpl.class);
}